// pages/shop/shop.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    currentTab: 0,
    categoryName: [], //商品种类
    goodsList: [], //商品列表
    height: 255, //列表高度
    flag: false, //是否显示购物车
    goodNum: 1, //商品数量
    goodsInfo:[],//商品信息
    goodCar:[],//购物车信息
  },
  //侧边导航跳转
  switchNav: function (e) {
    var page = this;
    var id = e.target.id;
    if (this.data.currentTab == id) {
      return false;
    } else {
      page.setData({
        currentTab: id
      });
    }
    page.setData({
      active: id
    });
    this.getGoodsList(e.target.dataset.id);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCategoryName();
    this.getGoodsList();
  },
  getCategoryName: function () {
    let _this = this;
    wx.request({
      url: 'https://dev.beiyouedu.com/cf/category/queryall',
      method: 'GET',
      success: function (res) {
        let result = res.data.data;
        if (res.data.code == 200) {
          _this.setData({
            categoryName: result,
          });
        }
      }
    });
  },
  getGoodsList: function (categoryId) {
    let _this = this;
    wx.request({
      url: 'https://dev.beiyouedu.com/cf/goods/wx/list',
      method: 'POST',
      data: {
        id: categoryId,
      },
      success: function (res) {
        let result = res.data.data;
        if (res.data.code == 200) {
          _this.setData({
            goodsList: result,
            height:result.length*240
          });
        }
      }
    });
  },
  //展示购物车
  showCar: function (parm) {
   let item=parm.currentTarget.dataset.item;
    this.setData({
      flag: true,
      goodsInfo:item,
      goodNum:1
    });
  },
  unshowCar: function () {
    this.setData({
      flag: false
    })
  },
  //增加商品数量
  addGoodNum: function () {
    let num = this.data.goodNum;
    if (num == 99) {
      wx.showToast({
        title: '不能再加啦...',
        icon: 'none',
        mask: true
      });
    } else {
      num++;
      this.setData({
        goodNum: num
      })
    }
  },
  //减少商品数量
  jianGoodNum: function () {
    let num = this.data.goodNum;
    if (num <= 1) {
      wx.showToast({
        title: '不能再减啦...',
        icon: 'none',
        mask: true
      })
    } else {
      num--;
      this.setData({
        goodNum: num
      })
    }
  },
//加入购物车
addToCar:function(){
  let goods={
    info:this.data.goodsInfo,
    num:this.data.goodNum
  }
  let goodsCar=wx.getStorageSync('shopItems');
  if(!goodsCar){
    goodsCar=[];
  }
  let flag=true;
  for(let i=0;i<goodsCar.length;i++){
    if(goods.info.id==goodsCar[i].info.id){
      goodsCar[i].num+=goods.num;
      flag=false;
      break;
    }
  }
  if(flag){
    goodsCar.push(goods);
  }
 wx.setStorageSync('shopItems', goodsCar);
  wx.showToast({
    title: '加入购物车成功...',
    icon: 'none',
    mask: true
  });
  this.setData({
    flag:false
  })
},
toGoCar:function(){
wx.navigateTo({
  url: '/pages/shop/goodsCar/goodsCar',
});
},
//跳转到商品详情
toGoodsInfo:function(goods){
  wx.navigateTo({
    url: '/pages/shop/good/goods?id='+goods.currentTarget.dataset.id
  })
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})